package com.hc.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.hc.config.TransactionResultHolder;
import com.hc.jian.JianMapper;
import com.hc.service.JianUserService;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JianUserServiceImpl implements JianUserService {
    @Autowired
    private JianMapper jianUserMapper;

    @Override
    public void incrementMoney(BusinessActionContext context, int userId, double money) {
        if (StringUtils.isEmpty(TransactionResultHolder.getResult(getClass(), context.getXid()))) {
            jianUserMapper.incrementMoney(userId, money);
            TransactionResultHolder.setResult(getClass(), context.getXid(), "xxxx");
        }
    }

    @Override
    public void commit(BusinessActionContext context) {
        if (!StringUtils.isEmpty(TransactionResultHolder.getResult(getClass(), context.getXid()))) {
            int userId = Integer.parseInt(context.getActionContext("userId").toString());
            double money = Double.parseDouble(context.getActionContext("money").toString());
            System.out.println("jian的commit⽅法执⾏了---userId=" + userId + "----money=" + money);
            jianUserMapper.commit(userId, money);
            //释放状态
            TransactionResultHolder.releaseResult(getClass(), context.getXid());
        }

    }

    @Override
    public void cancel(BusinessActionContext context) {

        if (!StringUtils.isEmpty(TransactionResultHolder.getResult(getClass(), context.getXid()))) {
            int userId = Integer.parseInt(context.getActionContext("userId").toString());
            double money = Double.parseDouble(context.getActionContext("money").toString());
            System.out.println("jian的cancel⽅法执⾏了---userId=" + userId + "----money=" + money);
            jianUserMapper.cancel(userId, money);
            //释放状态
            TransactionResultHolder.releaseResult(getClass(), context.getXid());
        }
    }
}
